package com.yx.system.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.yx.common.annotation.AuthIgnore2;
import com.yx.common.utils.Query;
import com.yx.common.utils.R;
import com.yx.modules.api.entity.TbUser;
import com.yx.modules.api.service.TbUserService;
import com.yx.system.dto.BookStatisticsDto;
import com.yx.system.entity.TdBook;
import com.yx.system.entity.TdCategory;
import com.yx.system.service.TdBookService;
import com.yx.system.service.TdCategoryService;
import com.yx.system.service.impl.TdOrderServiceImpl;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 后台管理统计
 * Created by wuye on 2017/12/13.
 */
@RestController
@RequestMapping("/sys/statis")
public class SysStatisticsController {

    @Autowired
    private TdOrderServiceImpl tdOrderService;

    @Autowired
    private TdBookService tdBookService;

    @Autowired
    private TbUserService tbUserService;

    @Autowired
    private TdCategoryService tdCategoryService;

    //查询月销售排行前五的小说
    @RequestMapping("/findBookTop")
    @RequiresPermissions("sys:statis:findBookTop")
    public R findBookTop() {
        Map<String, Object> result = new HashMap<>();
        //查询月销售榜
        List<TdBook> tdBookListSell = tdBookService.queryByDay();
        result.put("tdBookListSellTop", tdBookListSell);
        //会员男女统计
        Map<String, Object> map = new HashMap<>();
        List<TbUser> tbUserList = tbUserService.queryList(map);
        int nan = 0;
        int nv = 0;
        for (int i = 0; i < tbUserList.size(); i++) {
            if (tbUserList.get(i).getSex()) {
                nan += 1;
            } else {
                nv += 1;
            }
        }
        result.put("nan", nan);
        result.put("nv", nv);
        //累计销售榜
        map.put("orby", 1);
        List<TdBook> tdBookList = tdBookService.queryList(map);
        result.put("tdBookList", tdBookList);
        //分类统计
        Map<String, Integer> categoryList = new HashMap<>();
        List<TdCategory> tdCategoryList = tdCategoryService.queryList(map);
        for (int i = 0; i < tdCategoryList.size(); i++) {
            map.put("categoryId", tdCategoryList.get(i).getId());
            List<TdBook> tdBookList1 = tdBookService.queryList(map);
            categoryList.put(tdCategoryList.get(i).getCategoryyname(), tdBookList1.size());
        }
        result.put("category", categoryList);
        return R.ok().put("result",result);
    }

    /**
     * 查询当月所有书籍销售统计
     * @return
     */
    @RequestMapping("/sameMonth")
    @RequiresPermissions("sys:statis:sameMonth")
    public R sameMonth(@RequestParam Map<String, Object> params){
        //查询列表数据
        Query query = new Query(params);
        Page<BookStatisticsDto> pageUtil = new Page<BookStatisticsDto>(query.getPage(), query.getLimit());
        Page<BookStatisticsDto> page = tdOrderService.sameMonth(pageUtil,query);
        return R.ok().put("page", page);
    }


    /**
     * 查询当月所有书籍销售统计
     * @return
     */
    @RequestMapping("/sameMonth2")
    @RequiresPermissions("sys:statis:sameMonth2")
    public R sameMonth2(@RequestParam Map<String, Object> params){
        try {
            String time = params.get("time").toString();
            if(!time.equals("")|| time != null){
                StringBuffer sb = new StringBuffer(time);
                sb.append("-01");
                time = sb.toString();
                params.put("time",time);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            //查询列表数据
            Query query = new Query(params);
            Page<BookStatisticsDto> pageUtil = new Page<BookStatisticsDto>(query.getPage(), query.getLimit());
            Page<BookStatisticsDto> page = tdOrderService.sameMonth2(pageUtil,query);
            return R.ok().put("page", page);
        }
    }


    /**
     * 查询所有书籍销售统计
     * @return
     */
    @RequestMapping("/allStatistics")
    @RequiresPermissions("sys:statis:allStatistics")
    public R allStatistics(@RequestParam Map<String, Object> params){
        //查询列表数据
        Query query = new Query(params);
        Page<BookStatisticsDto> pageUtil = new Page<BookStatisticsDto>(query.getPage(), query.getLimit());
        Page<BookStatisticsDto> page = tdOrderService.allStatistics(pageUtil,query);
        return R.ok().put("page", page);
    }
}
